package mypackage.dao.impl;

import mypackage.bean.Clients;
import mypackage.dao.ClientsDao;
import mypackage.servicce.ClientsService;
import mypackage.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

/**
 * @author YH925
 * @create 2021/12/5 21:34
 */
public class ClientsImpl implements ClientsDao {
    Connection connection = null;

    {
        try {
            connection = JDBCUtils.getConnection();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    QueryRunner runner = new QueryRunner();
    @Override
    public Clients clientsLogin(Clients clients) {
        String sql = "select * from Clients where name = ? and password = ?";
        BeanHandler<Clients>handler = new BeanHandler<>(Clients.class);
        Clients _clients = null;
        try {
            _clients = runner.query(connection, sql, handler,clients.getName(),clients.getPassword());
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return _clients;
    }

    //注册员工信息
    @Override
    public void clientRegister(String username, String password) {
        try {
            connection.setAutoCommit(false);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        String sql = "insert into clients values(?, ?, ?)";
        try {
            runner.update(connection,sql,null,username,password);
            connection.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException ex) {
                ex.printStackTrace();
            }
        }
    }

    //根据用户名查找用户
    @Override
    public Clients selectClientByName(String username) {
        String sql = "select * from clients where name = ?";
        BeanHandler<Clients>handler = new BeanHandler<>(Clients.class);
        Clients client = null;
        try {
            client = runner.query(connection, sql, handler,username);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return client;
    }

    @Override
    public Clients findClientById(int id) {
        String sql = "select * from Clients where id = ?";
        BeanHandler<Clients>handler = new BeanHandler<>(Clients.class);
        Clients client = null;
        try {
            client = runner.query(connection, sql, handler, id);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return client;
    }

    @Override
    public List<Clients> findAllClients() {
        String sql = "select * from clients";
        BeanListHandler<Clients>handler = new BeanListHandler<>(Clients.class);
        List<Clients> clientsList = null;
        try {
            clientsList = runner.query(connection, sql, handler);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return clientsList;
    }
}
